<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改告警规则信息')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<input id="oldEventCode" type="hidden" th:field="${info.eventCode}"/>
		<form class="form-horizontal m" id="form-post-edit" th:object="${info}">
			<input id="id" name="id" type="hidden" th:field="*{id}"/>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">告警规则名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="ruleName" id="ruleName" th:field="*{ruleName}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">规则类型：</label>
				<div class="col-sm-8">
					<select id="ruleType" name="ruleType" class="form-control" th:with="type=${@dict.getType('alarm_rule_list')}" onchange="changeSelect()" required>
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${dict.dictValue == info.ruleType}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">事件编号：</label>
				<div class="col-sm-8">
					<select id="eventCode" name="eventCode" class="form-control" required>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">告警阈值：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="threshold" id="threshold" th:field="*{threshold}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">告警等级：</label>
				<div class="col-sm-8">
					<select id="level" name="level" class="form-control" th:with="type=${@dict.getType('alarm_level')}" required>
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${dict.dictValue == level}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">连续次数：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="continuousNumber" id="continuousNumber" th:field="*{continuousNumber}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">机器是否参与：</label>
				<div class="col-sm-8">
					<label class="toggle-switch switch-solid">
						<input type="checkbox" id="machineParticipation" th:checked="*{machineParticipation != 0}">
						<span></span>
					</label>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "system/alarmRule";

		$(function () {
			changeSelect($("#oldEventCode").val());
		});

		$("#form-post-edit").validate({
			onkeyup: false,
			rules:{
			},
			messages: {
		    },
		    focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
				let data = $('#form-post-edit').serializeArray();
				var machineParticipation = $("input[id='machineParticipation']").is(':checked') == true ? 1 : 0;
				data.push({"name": "machineParticipation", "value": machineParticipation});
				$.operate.save(prefix + "/edit", data);
	        }
	    }

		function changeSelect(eventCode) {
			//获取被选中的option值
			var ruleType = $('#ruleType').find('option:selected').val();
			$('#eventCode').empty();
			$.get(ctx + "system/dict/data/getType?dictType=" + ruleType, function (data) {
				var list = data.data;
				//遍历回传的数据添加到二级select
				$.each(list, function (key, info) {
					var option = '<option value="{0}" {1}>{2}</option>'.format(info.dictValue, info.dictValue == eventCode ? "selected" : "", info.dictLabel)
					$("#eventCode").append(option)
				});
			});
		}

	</script>
</body>
</html>
